﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Pages_Approval : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string pageType = "";
            if (Request.QueryString != null && Request.QueryString.Count > 0)
            {
                pageType = (Request.QueryString["Type"].ToString());
                DisplayPage(pageType);
            }
        }
    }
    private void DisplayPage(string pageType)
    {
        try
        {
            switch (pageType)
            {
                case Constants.Employee:
                    DisplayEmployee();
                    break;
                case Constants.Truck:
                    DisplayVehicle();
                    break;
                case Constants.Company:
                    DisplayCompany();
                    break;
                case Constants.Challan:
                    DisplayChallan();
                    break;
                case Constants.Material:
                    DisplayMaterial();
                    break;
                case Constants.Bill:
                    DisplayBill();
                    break;
                default:
                    break;
            }
        }
        catch
        {
        }
    }

    private void DisplayBill()
    {
        dvBill.Visible = true;
        ListCompany(ddlTransport, BusinessClass.CompanyType.Transport);
      //  ListBillNos();
    }

    private void DisplayChallan()
    {
        dvChallan.Visible = true;
        CallListPendingChallans();
    }

    private void CallListPendingChallans()
    {
        DataTable dt = new DataTable();
        dt = DataAccessClass.GetPendingChallan();
        if (dt.Rows.Count > 0)
        {
            ddlChallan.DataSource = dt.DefaultView;
            ddlChallan.DataTextField = "ChallanNo";
            ddlChallan.DataValueField = "ChallanId";
            ddlChallan.DataBind();
            int chId = 0;
            chId = Convert.ToInt32(ddlChallan.SelectedValue);
            ListChallanDetails(chId);
        }
        else
        {
            dvChallan.Visible = false;
            dvMsg.Visible = true;
            lblMsg.Text = "No Pending Challans, No Challans to approve";
        }
    }

    private void DisplayCompany()
    {
        dvCompany.Visible = true;
        CallListPendingCompanies();
    }

    private void CallListPendingCompanies()
    {
        DataTable dt = new DataTable();
        dt = DataAccessClass.GetPendingCompany();
        if (dt.Rows.Count > 0)
        {
            ddlCompany.DataSource = dt.DefaultView;
            ddlCompany.DataTextField = "Company";
            ddlCompany.DataValueField = "CompanyId";
            ddlCompany.DataBind();
        }
        else
        {
            dvCompany.Visible = false;
            dvMsg.Visible = true;
            lblMsg.Text = "No Pending Companies, No Company to approve";
        }
    }

    private void DisplayVehicle()
    {
        dvVehicle.Visible = true;
        CallListPendingVehicles();
    }

    private void CallListPendingVehicles()
    {
        DataTable dt = new DataTable();
        dt = DataAccessClass.GetPendingVehicle();
        if (dt.Rows.Count > 0)
        {
            ddlVehicle.DataSource = dt.DefaultView;
            ddlVehicle.DataTextField = "Vehicle";
            ddlVehicle.DataValueField = "VehicleId";
            ddlVehicle.DataBind();
        }
        else
        {
            dvVehicle.Visible = false;
            dvMsg.Visible = true;
            lblMsg.Text = "No Pending Companies, No Company to approve";
        }
    }

    private void DisplayEmployee()
    {
        dvEmployee.Visible = true;
        CallListPendingEmployees();
    }
    private void DisplayMaterial()
    {
        dvMaterial.Visible = true;
        CallListPendingMaterials();
    }


    private void CallListPendingEmployees()
    {
        DataTable dt = new DataTable();
        dt = DataAccessClass.GetPendingEmployees();
        if (dt.Rows.Count > 0)
        {
            ddlEmployee.DataSource = dt.DefaultView;
            ddlEmployee.DataTextField = "Employee";
            ddlEmployee.DataValueField = "EmployeeId";
            ddlEmployee.DataBind();
        }
        else
        {
            dvEmployee.Visible = false;
            dvMsg.Visible = true;
            lblMsg.Text = "No Pending Employees, No EMployee to approve";
        }
    }
    protected void btnDeleteBill_Click(object sender, EventArgs e)
    {
        try
        {
            int id = Convert.ToInt32(ddlBillNos.SelectedValue);
            BusinessClass.DeleteMaster(Constants.Bill, id, 0);
            ListBillNos();
        }
        catch (Exception ex)
        {

            throw;
        }
    }
    protected void lbnLogOut_Click(object sender, EventArgs e)
    {
        try
        {
            Session.Clear();
            Session.Abandon();
            Response.Redirect("../Login.aspx");
        }
        catch (Exception ex)
        {

        }
    }
    protected void btnApproveComp_Click(object sender, EventArgs e)
    {
        try
        {
            int userId = Convert.ToInt32(Session["UserId"]);
            int itemId = Convert.ToInt32(ddlCompany.SelectedValue);
            int res = 0;
            res = DataAccessClass.ApproveDetails("C", itemId, userId, 2, "");
            if (res > 0)
            {
                CallListPendingCompanies();
                trMessage.Visible = true;
            }
            else
            {
                trMessage.Visible = false;
                trError.Visible = true;

            }
        }
        catch (Exception ex)
        {
            trMessage.Visible = false;
            trError.Visible = true;
            trError.InnerText = ex.Message;
        }
    }
    protected void btnApproveEmp_Click(object sender, EventArgs e)
    {
        try
        {
            int userId = Convert.ToInt32(Session["UserId"]);
            int itemId = Convert.ToInt32(ddlEmployee.SelectedValue);
            int res = 0;
            res = DataAccessClass.ApproveDetails("E", itemId, userId, 2, "");
            if (res > 0)
            {
                CallListPendingEmployees();
                trMessage.Visible = true;
            }
            else
            {
                trMessage.Visible = false;
                trError.Visible = true;

            }
        }
        catch (Exception ex)
        {
            trMessage.Visible = false;
            trError.Visible = true;
            trError.InnerText = ex.Message;
        }
    }
    protected void btnApproveVehivcle_Click(object sender, EventArgs e)
    {
        try
        {
            int userId = Convert.ToInt32(Session["UserId"]);
            int itemId = Convert.ToInt32(ddlVehicle.SelectedValue);
            int res = 0;
            res = DataAccessClass.ApproveDetails("V", itemId, userId, 2, "");
            if (res > 0)
            {
                CallListPendingVehicles();
                trMessage.Visible = true;
            }
            else
            {
                trMessage.Visible = false;
                trError.Visible = true;

            }
        }
        catch (Exception ex)
        {
            trMessage.Visible = false;
            trError.Visible = true;
            trError.InnerText = ex.Message;
        }
    }
    protected void btnApproveChalan_Click(object sender, EventArgs e)
    {
        try
        {
            int userId = Convert.ToInt32(Session["UserId"]);
            int itemId = Convert.ToInt32(ddlChallan.SelectedValue);
            int res = 0;
            res = DataAccessClass.ApproveDetails("CH", itemId, userId, 2, "");
            if (res > 0)
            {
                CallListPendingChallans();
                trMessage.Visible = true;
            }
            else
            {
                trMessage.Visible = false;
                trError.Visible = true;

            }
        }
        catch (Exception ex)
        {
            trMessage.Visible = false;
            trError.Visible = true;
            trError.InnerText = ex.Message;
        }
    }
    protected void btnApproveAll_Click(object sender, EventArgs e)
    {
        try
        {
            int userId = Convert.ToInt32(Session["UserId"]);

            int itemId = 0;
            int res = 0;
            foreach (ListItem itm in ddlCompany.Items)
            {
                itemId = Convert.ToInt32(itm.Value);
                res = DataAccessClass.ApproveDetails("C", itemId, userId, 2, "");
            }
            if (res > 0)
            {
                CallListPendingCompanies();
                trMessage.Visible = true;
            }
            else
            {
                trMessage.Visible = false;
                trError.Visible = true;

            }
        }
        catch
        {
        }
    }
    protected void btnApproveAllEmp_Click(object sender, EventArgs e)
    {
        try
        {
            int userId = Convert.ToInt32(Session["UserId"]);

            int itemId = 0;
            int res = 0;
            foreach (ListItem itm in ddlEmployee.Items)
            {
                itemId = Convert.ToInt32(itm.Value);
                res = DataAccessClass.ApproveDetails("E", itemId, userId, 2, "");
            }
            if (res > 0)
            {
                CallListPendingEmployees();
                trMessage.Visible = true;
            }
            else
            {
                trMessage.Visible = false;
                trError.Visible = true;

            }
        }
        catch
        {
        }
    }
    protected void btnApproveAllVeh_Click(object sender, EventArgs e)
    {
        try
        {
            int userId = Convert.ToInt32(Session["UserId"]);

            int itemId = 0;
            int res = 0;
            foreach (ListItem itm in ddlVehicle.Items)
            {
                itemId = Convert.ToInt32(itm.Value);
                res = DataAccessClass.ApproveDetails("V", itemId, userId, 2, "");
            }
            if (res > 0)
            {
                CallListPendingVehicles();
                trMessage.Visible = true;
            }
            else
            {
                trMessage.Visible = false;
                trError.Visible = true;

            }
        }
        catch
        {
        }
    }
    protected void btnApproveAllChalan_Click(object sender, EventArgs e)
    {
        try
        {
            int userId = Convert.ToInt32(Session["UserId"]);

            int itemId = 0;
            int res = 0;
            foreach (ListItem itm in ddlChallan.Items)
            {
                itemId = Convert.ToInt32(itm.Value);
                res = DataAccessClass.ApproveDetails("CH", itemId, userId, 2, "");
            }
            if (res > 0)
            {
                CallListPendingChallans();
                trMessage.Visible = true;
            }
            else
            {
                trMessage.Visible = false;
                trError.Visible = true;

            }
        }
        catch
        {
        }
    }
    protected void ddlChallan_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            int chId = 0;
            chId = Convert.ToInt32(((DropDownList)(sender)).SelectedValue);
            ListChallanDetails(chId);
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }

    private void ListChallanDetails(int chId)
    {
        DataTable dt = new DataTable();
        Challan ch = new Challan(chId, 0);
        lblchno.Text = ch.ChallanNo.ToString();
        lblDate.Text = ch.ChallanDate.ToString();
        lblTransport.Text = ch.TransComp.ToString();
        lblSupplier.Text = ch.SuppComp.ToString();
        lblContractor.Text = ch.ContComp.ToString();
        lblTruckNo.Text = ch.TruckNo.ToString();
        lblDriver.Text = ch.Driver.ToString();
        lblCleaner.Text = ch.Cleaner.ToString();
        lblCFT.Text = ch.CFTVal.ToString();
        lblMaterial.Text = ch.MaterialDetail;
        lblTicketNo.Text = ch.TicketNo.ToString();
        imgChallan.ImageUrl = ImageURL(chId.ToString(), "CH", 1);
        anchor.HRef =  ImageURL(chId.ToString(), "CH", 1) ;
       
    }

    public string ImageURL(string img_id, string type, int imgNo)
    {
        return ("GetImage.aspx?id=" + img_id + "&Type=" + type + "&ImageNo=" + imgNo);
    }
    protected void btnRejectChallan_Click(object sender, EventArgs e)
    {
        try
        {
            int userId = Convert.ToInt32(Session["UserId"]);
            int itemId = Convert.ToInt32(ddlChallan.SelectedValue);
            int res = 0;
            res = DataAccessClass.ApproveDetails("CH", itemId, userId, 3, txtComment.Text.Trim());
            if (res > 0)
            {
                CallListPendingChallans();
                trMessage.Visible = true;
                lblAMsg.Text = "Record Rejected Successfully";

            }
            else
            {
                trMessage.Visible = false;
                trError.Visible = true;

            }
        }
        catch (Exception ex)
        {
            trMessage.Visible = false;
            trError.Visible = true;
            trError.InnerText = ex.Message;
        }
    }

    protected void btnApproveMaterial_Click(object sender, EventArgs e)
    {


        try
        {
            int userId = Convert.ToInt32(Session["UserId"]);
            int itemId = Convert.ToInt32(ddlMaterial.SelectedValue);
            int res = 0;
            res = DataAccessClass.ApproveDetails("M", itemId, userId, 2, "");
            if (res > 0)
            {
                CallListPendingMaterials();
                trMessage.Visible = true;
            }
            else
            {
                trMessage.Visible = false;
                trError.Visible = true;

            }
        }
        catch (Exception ex)
        {
            trMessage.Visible = false;
            trError.Visible = true;
            trError.InnerText = ex.Message;
        }
    }

    protected void btnApprovewBill_Click(object sender, EventArgs e)
    {


        try
        {
            if (ddlBillNos.Items.Count > 0)
            {


                int userId = Convert.ToInt32(Session["UserId"]);
                int itemId = Convert.ToInt32(ddlBillNos.SelectedValue);
                int res = 0;
                res = DataAccessClass.ApproveDetails("B", itemId, userId, 2, "");
                if (res > 0)
                {
                    ListBillNos();
                    trMessage.Visible = true;
                }
                else
                {
                    trMessage.Visible = false;
                    trError.Visible = true;

                }
            }
        }
        catch (Exception ex)
        {
            trMessage.Visible = false;
            trError.Visible = true;
            trError.InnerText = ex.Message;
        }
    }

    private void CallListPendingMaterials()
    {
        DataTable dt = new DataTable();
        dt = DataAccessClass.GetPendingMaterial();
        if (dt.Rows.Count > 0)
        {
            ddlMaterial.DataSource = dt.DefaultView;
            ddlMaterial.DataTextField = "Material";
            ddlMaterial.DataValueField = "MaterialId";
            ddlMaterial.DataBind();
        }
        else
        {
            dvMaterial.Visible = false;
            dvMsg.Visible = true;
            lblMsg.Text = "No Pending Material, No Material to approve";
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {

        try
        {
            int userId = Convert.ToInt32(Session["UserId"]);

            int itemId = 0;
            int res = 0;
            foreach (ListItem itm in ddlMaterial.Items)
            {
                itemId = Convert.ToInt32(itm.Value);
                res = DataAccessClass.ApproveDetails("M", itemId, userId, 2, "");
            }
            if (res > 0)
            {
                CallListPendingMaterials();
                trMessage.Visible = true;
            }
            else
            {
                trMessage.Visible = false;
                trError.Visible = true;

            }
        }
        catch
        {
        }
    }
    protected void btnRejectMa_Click(object sender, EventArgs e)
    {

    }

    private void CreateBill(DataSet ds1)
    {
        Session["dsBillDetails"] = ds1;
        this.ClientScript.RegisterClientScriptBlock(this.GetType(), "Window", "window.open('BillDetails.aspx','Bill','toolbar=0,resizable=0,scrollbars=1');", true);
    }

    protected void btnViewBill_Click(object sender, EventArgs e)
    {
        int billNo = 0;
        if (ddlBillNos.Items.Count > 0)
        {
            billNo = Convert.ToInt32(ddlBillNos.SelectedValue);
            DataSet ds1 = new DataSet();
            ds1 = DataAccessClass.GenerateBill(null, billNo);
            //  CreateWordFile(ds1);
            CreateBill(ds1);
        }
    }

    private void ListCompany(DropDownList ddl, BusinessClass.CompanyType companyType)
    {
        try
        {
            DataTable dt = new DataTable();
            dt = DataAccessClass.GetCompany(companyType);
            ddl.DataSource = dt.DefaultView;
           
            ddl.DataTextField = "CompanyName";
            ddl.DataValueField = "CompanyId";
            ddl.DataBind();
            ddl.Items.Add(new ListItem("Please Select", "0", true));
            ddl.SelectedValue = "0";
        }
        catch (Exception ex)
        {
        }
    }
    private void ListBillNos()
    {
        try
        {
            System.Data.DataTable dt = new System.Data.DataTable();
            dt = DataAccessClass.GetBillNos(true,Convert.ToInt32(ddlTransport.SelectedValue));
            ddlBillNos.DataSource = dt.DefaultView;
            ddlBillNos.DataTextField = "BillDetail";
            ddlBillNos.DataValueField = "BillNo";
            ddlBillNos.DataBind();
        }
        catch (Exception ex)
        {
        }
    }
    protected void ddlTransport_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlTransport.SelectedValue!="0")
        {
            ListBillNos();
            btnViewBill.Enabled = true;
            btnApproveBill.Enabled = true;
            btnDeleteBill.Enabled = true;
        }
        else
        {
            ddlBillNos.DataSource = new object[] { };
            ddlBillNos.DataBind();
            btnViewBill.Enabled = false;
            btnApproveBill.Enabled = false;
            btnDeleteBill.Enabled = false;
        }
       
    }
}